package ui;

import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;

public class QueryValidateRecordPanel extends JPanel {

    private static final Border border;
    private static final JTextField queryCode = new JTextField(20);
    private static final JTextField queryName = new JTextField(20);
    private JPanel topPanel;
    private JPanel bottomPanel;
    private DefaultTableModel dtm;
    private JButton queryButton;
    private JButton clearButton;
    private Vector<Vector<String>> tableData = new Vector<>();
    private DataTable table;
    private JScrollPane moddlePanel;

    static {
        border = BorderFactory.createLineBorder(Color.BLACK);
    }

    public QueryValidateRecordPanel(){
        this.setLayout(new BorderLayout());
        this.setTop();
        this.setMiddle();
        this.setBottom();
    }

    private void setTop(){
        this.topPanel = new JPanel();
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JLabel queryLabel1 = new JLabel("反馈信息代码:");
        queryLabel1.setBounds(10,20,80,25);
        jPanel1.add(queryLabel1);

        this.queryCode.setBounds(50,20,165,25);
        jPanel1.add(queryCode);

        JLabel queryLabel12= new JLabel("反馈标签:");
        queryLabel1.setBounds(215,20,80,25);
        jPanel1.add(queryLabel12);

        this.queryName.setBounds(300,20,165,25);
        jPanel1.add(queryName);

        JLabel tips = new JLabel("TIPS:反馈信息代码和反馈标签均支持模糊查询和精准查询，不输入则为全部查询");
        tips.setForeground(Color.red);
        jPanel2.add(tips);
        topPanel.setLayout(new GridLayout(2, 1));

        topPanel.add(jPanel1);
        topPanel.add(jPanel2);

        this.add(this.topPanel,"North");
    }

    public void acturalFun(){
        try{
            QueryValidateFun qtf = new QueryValidateFun(QueryValidateRecordPanel.this.table,queryCode.getText(),queryName.getText());
            qtf.paddingTableData(QueryValidateRecordPanel.this.table,queryCode.getText(),queryName.getText());
        }catch (NullPointerException e){
            JOptionPane.showMessageDialog(this.moddlePanel, "空指针异常", "错误",JOptionPane.WARNING_MESSAGE);
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(this.moddlePanel, "找不到查询文件", "错误",JOptionPane.WARNING_MESSAGE);
        }catch (IOException e) {
            JOptionPane.showMessageDialog(this.moddlePanel, "查询文件读取错误", "错误",JOptionPane.WARNING_MESSAGE);
        }catch (RuntimeException e){
            JOptionPane.showMessageDialog(this.moddlePanel, e.getMessage(), "错误",JOptionPane.WARNING_MESSAGE);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this.moddlePanel, "其他类型错误"+e.getMessage(), "错误",JOptionPane.WARNING_MESSAGE);
        }finally {
            this.queryCode.setText("");
            this.queryName.setText("");
        }
    }

    public void setMiddle(){
        this.dtm = new DefaultTableModel();
        this.dtm.setDataVector(this.tableData,new Vector(Arrays.asList(DataTable.TABLE_HEADER_VALIDATE_NAME)));
        this.table = new DataTable(this.dtm,2);
        this.moddlePanel = new JScrollPane(this.table);
        this.moddlePanel.setBorder(border);
        this.add(this.moddlePanel,"Center");
    }

    public void setBottom(){
        this.bottomPanel = new JPanel();
        bottomPanel.setBorder(border);
        this.queryButton = new JButton("查询(ENTER)");
        queryButton.registerKeyboardAction(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                acturalFun();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                acturalFun();
            }
        });
        bottomPanel.add(this.queryButton);
        this.add(this.bottomPanel,"South");
    }


}
